package app.project.streetlensproject.adapter;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import app.project.streetlensproject.R;
import app.project.streetlensproject.model.Place;
import app.project.streetlensproject.util.AppUtils;

public class ItemListPlaceAdapter extends BaseAdapter {

	private ArrayList<Place> mArrPlaces;
	private LayoutInflater mLayoutInflater;
	private float angleRotate; // angle when sensor change
	private double locationX, locationY;

	public ItemListPlaceAdapter(Context context, ArrayList<Place> places, float angle) {
		this.mArrPlaces = places;
		this.angleRotate = angle;
		mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public ItemListPlaceAdapter(Context context, ArrayList<Place> places, double locationX,
			double locationY) {
		this.locationX = locationX;
		this.locationY = locationY;
		this.mArrPlaces = places;
		try {
			mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		} catch (Exception e) {
		}
	}

	public int getCount() {
		return mArrPlaces.size();
	}

	public Place getItem(int arg0) {
		return mArrPlaces.get(arg0);
	}

	public long getItemId(int arg0) {
		return 0;
	}

	public class ViewHolder {
		public TextView textViewName;
		public TextView textViewDistance;
		public ImageView imageViewCompass;
		public ImageView imageViewPlace;
	}

	@SuppressLint("NewApi")
	public View getView(final int position, View convertView, ViewGroup parent) {
		final ViewHolder view;
		if (convertView == null) {
			view = new ViewHolder();
			convertView = mLayoutInflater.inflate(R.layout.item_list_places,
					null);

			view.textViewName = (TextView) convertView
					.findViewById(R.id.textViewNamePlace);
			view.textViewDistance = (TextView) convertView
					.findViewById(R.id.textViewDistance);
			view.imageViewCompass = (ImageView) convertView
					.findViewById(R.id.imageViewCompass);
			view.imageViewPlace=(ImageView)convertView.findViewById(R.id.imageViewPlace);

			convertView.setTag(view);
		} else {
			view = (ViewHolder) convertView.getTag();
		}
		Place place = getItem(position);  
		view.textViewName.setText(mArrPlaces.get(position).placename);
		float distance = (AppUtils.DistancePlace(locationX, locationY, place.locationx, place.locationy));
		view.textViewDistance.setText(distance+" km");
		view.imageViewCompass.setBackgroundResource(R.drawable.icn_direction_detail);
		view.imageViewCompass.setRotation(mArrPlaces.get(position).updateAngleRotate());
		if(mArrPlaces.get(position).getBitmap()==null){
			view.imageViewPlace.setBackgroundResource(R.drawable.no_image);
		}else{
			view.imageViewPlace.setImageBitmap(place.getBitmap());
		}
		return convertView;
	}
}
